﻿<?php if (!defined('SERVER_ROOT')) exit('No direct script access allowed');

class nckh_Model extends Model {

    function __construct()
    {
        parent::__construct();
    }
    public function qry_all_nckh()
    {
        return $this->db->ExecuteCursor("BEGIN CONGTAC2.SELECT_NCKH(:CUR); END;",'CUR');
    }
    public function qry_single_nckh($TEN_NCKH)
    {
       $P_MA_CAN_BO = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
       return $this->db->ExecuteCursor("BEGIN CONGTAC2.SELECT_NCKH_DETAIL(:CUR,$P_MA_CAN_BO,'$TEN_NCKH'); END;",'CUR');
    }
    public function update_nckh()
    {
        $P_UPDATE_TYPE = get_post_var('hdn_update_type');
        $P_NGUOI_CAP_NHAT = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_CAN_BO = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_TEN_HOC_KY = get_post_var('txt_TEN_HOC_KY');
        $P_MA_LOAI_NCKH = get_post_var('cbx_MA_LOAI_NCKH');
        $P_TEN_NCKH = get_post_var('txt_TEN_NCKH');
        $P_CAP_BAC = get_post_var('txt_CAP_BAC');
        $P_SO_TIEN = get_post_var('txt_SO_TIEN');

        $stmt = $this->db->PrepareSP("BEGIN CONGTAC2.UPDATE_NCKH(:P_VAL,
                                                                :P_UPDATE_TYPE,
                                                                :P_NGUOI_CAP_NHAT,
                                                                :P_MA_CAN_BO,
                                                                :P_TEN_HOC_KY,
                                                                :P_MA_LOAI_NCKH,
                                                                :P_TEN_NCKH,
                                                                :P_CAP_BAC,
                                                                :P_SO_TIEN
                                                                );END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_UPDATE_TYPE,'P_UPDATE_TYPE');
        $this->db->InParameter($stmt,$P_NGUOI_CAP_NHAT,'P_NGUOI_CAP_NHAT');
        $this->db->InParameter($stmt,$P_MA_CAN_BO,'P_MA_CAN_BO');
        $this->db->InParameter($stmt,$P_TEN_HOC_KY,'P_TEN_HOC_KY');
        $this->db->InParameter($stmt,$P_MA_LOAI_NCKH,'P_MA_LOAI_NCKH');
        $this->db->InParameter($stmt,$P_TEN_NCKH,'P_TEN_NCKH');
        $this->db->InParameter($stmt,$P_CAP_BAC,'P_CAP_BAC');
        $this->db->InParameter($stmt,$P_SO_TIEN,'P_SO_TIEN');
        $chaythutuc = $this->db->Execute($stmt);
        //echo '<script>alert("'.$P_VAL.'");</script>';
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->popup_exec_done();
          }elseif($P_VAL == 'TON_TAI_NCKH'){
              $this->popup_exec_fail(__('Tên NCKH đã tồn tại!'));
              return;
          }else{
               $this->popup_exec_fail(__('Không thể cập nhật!'));
               return;
          }
        }
    }
    public function delete_nckh(){
        $P_NGUOI_XOA = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_TEN_NCKH = get_post_var('hdn_item_id',0);
        //echo '<script>alert("'.$P_MA_NCKH.'");</script>';
        $stmt = $this->db->PrepareSP("BEGIN CONGTAC2.DELETE_NCKH(:P_VAL,:P_NGUOI_XOA,:P_TEN_NCKH);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_NGUOI_XOA,'P_NGUOI_XOA');
        $this->db->InParameter($stmt,$P_TEN_NCKH,'P_TEN_NCKH');
        $chaythutuc = $this->db->Execute($stmt);
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->exec_done($this->goback_url);
          }else{
               $this->popup_exec_fail(__('Không thể xóa!'));
               return;
          }
        }
    }
}